package com.konsung.util;

import android.util.Base64;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 单位转换工具类
 *
 */
public class UnitConvertUtil {

    /**
     * 私有构造
     */
    private UnitConvertUtil() {

    }

    /**
     * Convert hex string to byte[]
     *
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * 字符转byte[]
     * @param hexString 字符
     * @return byte[]
     */
    public static byte[] getByteformHexString(String hexString) {
        //去最高位
        byte[] bytes = hexStringToBytes(hexString);
        return changeByte(bytes);
    }
    /**
     * 转换波形数据
     * @param value 波形数据
     * @return 转换后的数据
     * @throws Exception 转换异常
     */
    public static int[] intValue(String value) throws Exception {
        byte[] out = Base64.decode(value.getBytes(), Base64.DEFAULT);
        byte[] f = new byte[4];
        f[3] = out[0];
        f[2] = out[1];
        f[1] = out[2];
        f[0] = out[3];
        List<Integer> vList = new ArrayList<Integer>();
        for (int i = 4; i < out.length; i++) {
            f = new byte[2];
            f[1] = out[i + 0];
            f[0] = out[i + 1];
            vList.add(toInt(f));
            i++;
        }
        int[] b = new int[vList.size()];
        for (int i = 0; i < vList.size(); i++) {
            b[i] = vList.get(i);
        }
        return b;
    }

    /**
     * 字节转换
     * @param a 字节
     * @return 转换后的字节
     */
    public static byte[] changeByte(byte[] a) {
        List<Integer> iList = new ArrayList<Integer>();
        int len = 0;
        int aLength = 0;
        if (a != null) {
            aLength = a.length;
        } else {
            aLength = 0;
        }
        if (aLength % 2 == 1) {
            len = 1;
        }
        for (int i = 4; i < aLength - len; i++) {
            byte[] f = new byte[2];
            f[0] = a[i + 0];
            f[1] = a[i + 1];
            int v = toInt(f);
            if (v > 4096) {
                v = v % 4096;
            }
            iList.add(v);
            i++;
        }

        byte[] d = new byte[4 + iList.size() * 2];
        byte[] fb = toByteArray(iList.size(), 4);
        d[0] = fb[3];
        d[1] = fb[2];
        d[2] = fb[1];
        d[3] = fb[0];
        for (int i = 0; i < iList.size(); i++) {
            fb = toByteArray(iList.get(i), 2);
            d[4 + i * 2] = fb[1];
            d[4 + i * 2 + 1] = fb[0];
        }
        return d;
    }

    /**
     * 字节转int
     * @param bRefArr 字节
     * @return int
     */
    public static int toInt(byte[] bRefArr) {
        int iOutcome = 0;
        byte bLoop;
        for (int i = 0; i < bRefArr.length; i++) {
            bLoop = bRefArr[i];
            iOutcome += (bLoop & 0xFF) << (8 * i);
        }
        return iOutcome;
    }

    /**
     * int转字节
     * @param iSource int
     * @param iArrayLen int
     * @return 字节
     */
    public static byte[] toByteArray(int iSource, int iArrayLen) {
        byte[] bLocalArr = new byte[iArrayLen];
        for (int i = 0; (i < 4) && (i < iArrayLen); i++) {
            bLocalArr[i] = (byte) (iSource >> 8 * i & 0xFF);
        }
        return bLocalArr;
    }

    /**
     * Convert char to byte
     *
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * 转javabean为json
     *
     * @param bean bean
     * @return jsonString
     */
    public static String objectToJSON(Object bean) {
        //返回的json
        String json = "{";
        //几个属性
        int count;
        //当前位置
        int position = 0;
        Class cl = bean.getClass();
        Field[] fields = cl.getDeclaredFields();
        count = fields.length;
        try {
            for (Field field : fields) {
                position++; //从第一个开始
                field.setAccessible(true);
                json += "\"" + field.getName() + "\":\"" + field.get(bean) +
                        "\"";
                if (position != count) {
                    json += ",";
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        json += "}";
        return json;

    }

    /**
     * Convert Map to JSON
     *
     * @param map map
     * @return to JSON
     */

    public static String mapToJSON(Map<String, Object> map) {
        String json = "{";
        int position = 0;
        for (String str : map.keySet()) {
            if (str.equals("recs")) {
                json += "\"" + str + "\":" + map.get(str);
            } else {
                json += "\"" + str + "\":" + "\"" + map.get(str) + "\"";
            }
            position++;
            if (position != map.keySet().size()) {
                json += ",";
            }
        }
        json += "}";

        return json;
    }

}
